package com.niuke;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

/**
 * 39. 跳格子游戏
 * https://blog.csdn.net/qq_34143141/article/details/131667933
 * https://blog.csdn.net/sadanmowang/article/details/127944037
 *
 */
public class NiukeMoni39 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int length = Integer.parseInt(scanner.nextLine());
        Map<Integer, List<Integer>> implement = new HashMap<>();
        
        Integer[] x = new Integer[length]; // 一个标记位数组

        for (int i = 0; i < length; i++) {
            String[] item = scanner.nextLine().split(",");
            Integer key = Integer.parseInt(item[0]);
            Integer value = Integer.parseInt(item[1]);

            List<Integer> temp = implement.getOrDefault(key, new ArrayList<>());
            temp.add(value);
            implement.put(key, temp);
            
            x[value] = key; // 说明当前step需要依赖其它step才能开启
        }

        Stack<Integer> stack = new Stack<>();
        
        for (int i = 0; i < x.length; i++) {
            if (x[i] == null) { // 说明当前step不需要依赖其它step，本身就是开启状态
                stack.add(i);
            }
        }
        
        if (stack.isEmpty()) {
            System.out.println(false);
            return;
        }
        
        while (!stack.isEmpty()) {
            Integer step = stack.pop();
            x[step] = -1; // 标记位置为-1，代表已经开启并达到过
            
            List<Integer> next = implement.get(step);
            if (next == null || next.size() == 0) {
                continue;
            }

            for (int i = 0; i < next.size(); i++) {
                if (x[next.get(i)] != -1) {
                    stack.add(next.get(i));
                }
            }
        }

        boolean result = true;
        for (int i = 0; i < x.length; i++) {
            if (x[i] != -1) { // 没走到
                result = false;
                break;
            }
        }
        System.out.println(result);

    }

}
